package com.lm.survey.logic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.lm.survey.model.ChoiceTemplate;
import com.lm.survey.model.GeneralOptionValue;
import com.lm.survey.model.SingleChoiceTemplate;
import com.lm.survey.util.DBPool;
import com.lm.survey.util.SurveyException;

public class ChoiceLogic {
	static Logger logger = Logger.getLogger(ChoiceLogic.class);

	public static void saveChoice(int itemId, ChoiceTemplate template,
			Connection con) {

		switch (template.getType()) {
		case 1: {
			new SingleChoiceLogic().saveSingleChoice(itemId,
					(SingleChoiceTemplate) template, con);
			break;

		}
		case 2: {
		    new SingleChoiceLogic().saveSingleChoice(itemId,
                    (SingleChoiceTemplate) template, con);
		    break;
		}
		}

	}
	
	public static Object   saveOptionalValue(int itemId,int type,HttpServletRequest request){
		switch (type) {
		case 1: {
		    String value = request.getParameter("value");
            int weight = Integer.parseInt(request.getParameter("weight"));
            GeneralOptionValue op = new GeneralOptionValue();
            op.setValue(value);
            op.setWeight(weight);
            new SingleChoiceLogic().addOptionalValue(itemId, op);
            return new Integer(op.getId()); 
		}
		case 2: {
		    String value = request.getParameter("value");
            int weight = Integer.parseInt(request.getParameter("weight"));
            GeneralOptionValue op = new GeneralOptionValue();
            op.setValue(value);
            op.setWeight(weight);
            new SingleChoiceLogic().addOptionalValue(itemId, op);
            return new Integer(op.getId()); 
        }
		}
		
		return null;
	}
	
	
	public static void   updateOptionalValue(int itemId,int type,HttpServletRequest request){
		switch (type) {
		case 1: {
			String value = request.getParameter("value");
			int weight = Integer.parseInt(request.getParameter("weight"));
			GeneralOptionValue op = new GeneralOptionValue();
			op.setValue(value);
			op.setWeight(weight);
			op.setId(Integer.parseInt(request.getParameter("id")));
			new SingleChoiceLogic().updateOptionValue(op);
			
		}
		case 2: {
		    String value = request.getParameter("value");
            int weight = Integer.parseInt(request.getParameter("weight"));
            GeneralOptionValue op = new GeneralOptionValue();
            op.setValue(value);
            op.setWeight(weight);
            op.setId(Integer.parseInt(request.getParameter("id")));
            new SingleChoiceLogic().updateOptionValue(op);
		}
		}
		
	}
	
	public static void   removeOptionalValue(int type,HttpServletRequest request){
		switch (type) {
		case 1: {
			
			int id = Integer.parseInt(request.getParameter("id"));
			new SingleChoiceLogic().removeOptionValue(id);
			
		}
		case 2: {
		    int id = Integer.parseInt(request.getParameter("id"));
            new SingleChoiceLogic().removeOptionValue(id);
		}
		}
		
	}
	

	public static ChoiceTemplate getTemplate(int itemId, int type,
			Connection con) {
		switch (type) {
		case 1: {
			return new SingleChoiceLogic().getTemplate(itemId, con);
			
		}
		case 2: {
		    return new SingleChoiceLogic().getTemplate(itemId, con);
		}
		}
		return null;
	}

	public static void removeAllChoice(int itemId, int type, Connection con) {

		switch (type) {
		case 1: {
			new SingleChoiceLogic().removeAllOptionValue(itemId, con);

		}
		case 2: {
		    new SingleChoiceLogic().removeAllOptionValue(itemId, con);
		}
		}

	}

}
